package com.gzsxy.rabbitmq.consumer.config.redis;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.StringRedisSerializer;


/**
 * @Description: redis配置类
 * @Author: xiaolong
 * @date: 2023/2/16 9:04
 * @Version: 1.0
 */
@Configuration
public class StarterConfiguration {

    /**
     * 我们自定义一个 RedisTemplate，设置序列化器，这样我们可以很方便的操作实例对象。
     * 将StringRedisTemplate默认key，hashkey都是object类型，转为string类型，并进行序列化
     */
    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {


        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(factory);
        //为了自己开发开发方便，一般直接使用<String, Serializable> 类型
//        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //value序列化
//        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //key序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }
}
